package com.fyzk.activity.user;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fyzk.activity.user.xiangxi.ZhuihaoDetailActivity;
import com.fyzk.bean.ChaseListItem;
import com.tyjc.lottery.R;
import com.fyzk.net.PublicMethod;
import com.fyzk.utils.IntentParamUtils;
import com.fyzk.utils.LogUtil;

import java.util.ArrayList;

public class ZhuiHaoListAdapter extends BaseAdapter {

    private ArrayList<ChaseListItem> infos;
    private Context context;

    public ZhuiHaoListAdapter(Context context, ArrayList<ChaseListItem> infos) {
        // TODO Auto-generated constructor stub
        this.context = context;
        this.infos = infos;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return infos.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return infos.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(
                    R.layout.user_chasebetrecord_item, null);
            holder.bg = (LinearLayout) convertView
                    .findViewById(R.id.user_exp_child_ll);
            holder.title1 = (TextView) convertView
                    .findViewById(R.id.user_exp_title_time);
            holder.title = (TextView) convertView
                    .findViewById(R.id.user_exp_child_title);
            holder.time = (TextView) convertView
                    .findViewById(R.id.user_exp_child_time);
            holder.state = (TextView) convertView
                    .findViewById(R.id.chasebet_all);
            holder.money = (TextView) convertView
                    .findViewById(R.id.user_exp_child_money);
            holder.cancel = (Button) convertView.findViewById(R.id.cancel_action);
            holder.prizeMoney = (TextView) convertView
                    .findViewById(R.id.chasebet_beted);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.bg.setClickable(true);

        final ChaseListItem info = infos.get(position);
        LogUtil.v(info.toString());
        holder.title.setText(info.getLotteryTypeName());
        String time;
        try {
            time = info.getCreateTime().substring(5, 16).replace(" ", "\n");
        } catch (Exception e) {
            time = info.getCreateTime();
        }
        holder.time.setText(time);
        holder.state.setText("共追" + info.getBatchNum() + "期");
        holder.prizeMoney.setText("已追" + info.getBetNum() + "期");
        holder.money.setText("总彩金" + PublicMethod.formatMoney(info.getTotalAmount()) + "米");
        if (info.getState() == 2) {
            holder.cancel.setEnabled(false);
            holder.cancel.setText("已取消追号");
        } else if (info.getState() == 3) {
            holder.cancel.setEnabled(false);
            holder.cancel.setText("已追完");
        } else {
            holder.cancel.setText("取消追号");
            holder.cancel.setEnabled(true);
        }
        holder.cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onCancelClickListener != null) {
                    onCancelClickListener.onCancel(info.getId());
                }
            }
        });

        holder.bg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context , ZhuihaoDetailActivity.class);
                intent.putExtra(IntentParamUtils.BETDETAILID, info.getId());
                intent.putExtra(IntentParamUtils.CHASE, info.getBatchNum());
                intent.putExtra(IntentParamUtils.STATE, info.getStateName());
                intent.putExtra(IntentParamUtils.PRIZEEND, info.isPrizeend());
                intent.putExtra(IntentParamUtils.AMT, info.getTotalAmount());
                intent.putExtra(IntentParamUtils.LOTTERYNAME, info.getLotteryTypeName());
                context.startActivity(intent);
            }
        });
        return convertView;
    }

    public void setOnCancelClickListener(OnCancelClickListener onCancelClickListener) {
        this.onCancelClickListener = onCancelClickListener;
    }

    private OnCancelClickListener onCancelClickListener;

    public interface OnCancelClickListener {
        void onCancel(String orderid);
    }


    class ViewHolder {
        LinearLayout bg;
        TextView title1;
        TextView title;
        TextView time;
        TextView state;
        TextView money;
        Button cancel;
        TextView prizeMoney;
    }

    enum ZhuiHaoStatus {
        YICHANG,
        ZHENGZAIZHUI,
        CHEDAN,
        ZHUIWAN
    }
}
